package com.hzqy.commons.service.utils;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.web.login.SessionKey;
import com.hzqy.commons.utils.SystemConfigUtils;
import edu.emory.mathcs.backport.java.util.Collections;


public class HotelAlliance {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	String cookiesValue = null;
	
	public HotelAlliance(String cookiesValue) {
		this.cookiesValue = cookiesValue;
	}
	
	public String getPriceHotel(String serverid,String timestamp,String secret,String source,int readTimeOut,int connectTimeout,HttpServletRequest request){
		String serverPriceUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.hotel_alliance_path_url.getKey());
		HttpSession session = request.getSession(true);
		String epg_info=String.valueOf(session.getAttribute(SessionKey.SESSION_EPG_INFO));
		String url = null;
		String sign = getSign(serverid,timestamp,secret,source);
		String Result = "";
		try{
		//	serverPriceUrl = "http://10.255.247.118:83/admin/itf/hotelItf/getProduct.do";
			url = serverPriceUrl+"?sign="+sign+"&timestamp="+timestamp+"&secret="+secret+"&source="+source+"&serverid="+serverid+"&epg_info="+epg_info;
			LOG.debug(url);
			Result = OrderXmlUtils.sendPost(url, readTimeOut, connectTimeout,this.cookiesValue);
		}catch(Exception e) {
			Result = "-1";
		}
		return Result;
	}
	
	public String getSign(String serverid,String timestamp,String secret,String source)
	{
		Map<String,String> signParams = new HashMap<String, String>();
		//1.1接口所有请求参数，演示sign生成方式
		signParams.put("serverid", serverid);
		signParams.put("timestamp", timestamp);
		signParams.put("secret", secret);
		signParams.put("source", source);
	  
		//2.1 获取signParams中的key值
		StringBuilder sb = new StringBuilder();
		List<String> keyList = new ArrayList(signParams.size());
		for (Map.Entry entry : signParams.entrySet()) {
			keyList.add((String) entry.getKey());
		}

		//2.2将key值进行排序
		Collections.sort(keyList);

		//2.3 根据排序后的key值按照下列各式输出
		sb.append(secret);
		for (String key : keyList) {
			sb.append(key).append((String)signParams.get(key));
		}

		sb.append(secret);

		//2.4 SHA1 加密后 将二进制输出为字符串
			try
			{
				byte[] sha1Digest = getSHA1Digest(sb.toString());
				return byte2hex(sha1Digest);
			} catch (IOException e) {
				throw new RuntimeException("签名失败", e);
			}       
	}

    private static byte[] getSHA1Digest(String data) throws IOException 
	{
		byte[] bytes = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			bytes = md.digest(data.getBytes("UTF-8"));
		} catch (GeneralSecurityException gse) {
			//throw new IOException(gse.getMessage());
		}
		return bytes;
    }

	private static String byte2hex(byte[] bytes) 
	{
        StringBuilder sign = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
				sign.append("0");
            }
            sign.append(hex.toUpperCase());
        }
        return sign.toString();
	}
}
